package com.nd.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.CollectMapper;
import com.nd.dao.GoodsMapper;
import com.nd.pojo.bean.Collect;
import com.nd.pojo.bean.CollectExample;
import com.nd.pojo.bean.Goods;

import com.nd.pojo.bean.User;
import com.nd.pojo.bo.wx.CollectAddrdleteBo;
import com.nd.pojo.bo.wx.CollectListBo;
import com.nd.pojo.vo.wx.CollectListBeanVo;
import com.nd.pojo.vo.wx.CollectListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author : PengFei Zhang
 * @Data :2021/6/7  11:24
 * @Description :
 */
//wx收藏  zpf
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    GoodsMapper goodsMapper;

    //商品收藏列表
    @Override
    public CollectListVo collectList(CollectListBo collectListBo) {
        //获取用户信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getId();
        //执行查询之前 👉 使用分页插件 👉 会在执行过程中拼接上sql语句  要引入插件
        PageHelper.startPage(collectListBo.getPage(), collectListBo.getSize());
        //构造条件
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        //把用户信息传入collectMapper  得到用户的所有收藏信息
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //建立collectListBeanvo的list
        List<CollectListBeanVo> collectListBeanVos = new ArrayList<>();
        CollectListVo collectListVo = new CollectListVo();
        for (Collect collect : collects) {
            //判断请求参数的type是否和collect表中的type是否一致
            int s=collect.getType();
            int s1=collectListBo.getType();
            if (s==s1) {
                Integer valueId = collect.getValueId();
                //把得到的信息封装入 CollectListBeanVo
                CollectListBeanVo collectListBeanVo = new CollectListBeanVo();
                collectListBeanVo.setValueId(valueId);
                collectListBeanVo.setId(collect.getId());
                collectListBeanVo.setType(collect.getType());
                //把得到的valueId传入商品表  商品表和收藏表之间通过valueId链接
                Goods goods = goodsMapper.selectByPrimaryKey(valueId);
                //得到商品信息
                if (goods!=null){
                    collectListBeanVo.setBrief(goods.getBrief());
                    collectListBeanVo.setPicUrl(goods.getPicUrl());
                    collectListBeanVo.setName(goods.getName());
                    collectListBeanVo.setRetailPrice(goods.getRetailPrice().doubleValue());
                    collectListBeanVos.add(collectListBeanVo);
                }
            }
        }

        //得到信息总条数
        PageInfo<Collect> pageInfo = new PageInfo<>(collects);
        int pages = pageInfo.getPages();
        //把得到的数据响应出去
        collectListVo.setTotalPages(pages);
        collectListVo.setCollectList(collectListBeanVos);
        return collectListVo;
    }

    //商品收藏
    @Override
    public String collectAddrdlete(CollectAddrdleteBo collectAddrdleteBo) {
        //获取用户信息
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        Integer userId = user.getId();
        //根据传入的请求参数，先进行查询
        //构造条件
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andValueIdEqualTo(collectAddrdleteBo.getValueId());
        //定义返回字符串
        String s=null;
        //查询
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        if (collects.size() != 0){
            //若集合不为0，则证明里面有数据  这时候就根据传入的参数 对其进行删除
            collectMapper.deleteByExample(collectExample);
            s="delete";
            return s;
        }
        //若查询不存在 就添加
        Collect collect = new Collect();
        collect.setUserId(userId);
        collect.setValueId(collectAddrdleteBo.getValueId());
        collectMapper.insertSelective(collect);
        s="add";
        return s;
    }

}
